package com.invoice.model.dao;

import com.invoice.exceptions.InvoiceException;
import java.io.Serializable;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class HibernateGenericDAO<T, ID extends Serializable> implements GenericDAO<T, ID> {

    private Class<T> persistentClass;
    protected String key;
    protected Session session;
    protected Transaction transaction;

     
    
    
    public HibernateGenericDAO(Class<T> persistentClass) {
        this.persistentClass = persistentClass;
    }

    public Class<T> getPersistentClass() {
        return persistentClass;
    }

    public void startSession() {
        session = com.invoice.model.dao.HibernateUtil.getSessionFactory().getCurrentSession();
        transaction = session.beginTransaction();
    }

    @Override
    public T get(final ID id) {
        startSession();
        Object obj = session.get(persistentClass, id);
        return (T) obj;
    }

    public <T> List<T> getAll(final Class<T> type) {
        startSession();
        final Criteria crit = session.createCriteria(type);
        return crit.list();

    }

    @Override
    public T obtenerPorId(ID id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void add(final T entity) {
        startSession();
        session.saveOrUpdate(entity);
        transaction.commit();
    }

  

}
